#include "options.h"
#include "ui_options.h"
#include <QDebug>


options::options(QWidget* parent) :
    QDialog(parent),
    ui(new Ui::options)
{
    QSettings Ini(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat, this);
    Ini.setIniCodec("UTF-8");
    ui->setupUi(this);

    //QStringList m_serialPortName;
    foreach(const QSerialPortInfo& info, QSerialPortInfo::availablePorts())
    {
        ui->portNameComboBox->addItem(info.portName());
    }

    ui->portNameComboBox->setCurrentText(Ini.value("com/portname").toString());
    ui->baudRateComboBox->setCurrentText(Ini.value("com/baudrate").toString());
    ui->checkBoxTimeout->setChecked(Ini.value("com/timeout", 1).toBool());
    ui->spinBox_Day->setValue(Ini.value("sys/day", 30).toInt());
    ui->spinBoxPoll->setValue(Ini.value("com/delytime", 20).toInt());
    ui->comboBox->setCurrentIndex(Ini.value("sys/mp3", 0).toInt());
    ui->rb_random->setChecked(Ini.value("sys/mp3_random", false).toBool());
    ui->editHostName->setText(Ini.value("net/hostname", "127.0.0.1").toString());
    ui->spinPort->setValue(Ini.value("net/port", 1883).toInt());
    ui->editUserName->setText(Ini.value("net/username").toString());
    ui->editTopic->setText(Ini.value("net/topic").toString());
    ui->editPassword->setText(Ini.value("net/password").toString());
    ui->checkThingsBoard->setChecked(Ini.value("net/tb", 0).toBool());
    standItemModel = new QStandardItemModel();
    ui->tableView->setModel(standItemModel);
    //添加表头
    standItemModel->setColumnCount(4);
    standItemModel->setRowCount(10);
    standItemModel->setHeaderData(0, Qt::Horizontal, QStringLiteral("卡槽号")); //设置表头内容
    standItemModel->setHeaderData(1, Qt::Horizontal, QStringLiteral("Modbus 地址"));
    standItemModel->setHeaderData(2, Qt::Horizontal, QStringLiteral("A通道名称"));
    standItemModel->setHeaderData(3, Qt::Horizontal, QStringLiteral("B通道名称"));

    for(int i = 1; i <= MAX_CARD_COUNT; i++)
    {
        standItemModel->setItem(i - 1, 0, new QStandardItem(QString::number(i) + "#卡槽"));
        standItemModel->setItem(i - 1, 1, new QStandardItem(Ini.value("msa/addr" + QString::number(i),
                                0).toString()));
        standItemModel->setItem(i - 1, 2, new QStandardItem(Ini.value("msa/a" + QString::number(i),
                                "区域A" + QString::number(i)).toString()));
        standItemModel->setItem(i - 1, 3, new QStandardItem(Ini.value("msa/b" + QString::number(i),
                                "区域B" + QString::number(i)).toString()));
        standItemModel->item(i - 1, 0)->setFlags((Qt::NoItemFlags)); //只读
    }
}

options::~options()
{
    delete ui;
}

void options::on_btn_save_clicked()
{
    QMessageBox::information(this, "注意", "重新连接后，修改才能生效。");
    QSettings Ini(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat, this);
    Ini.setValue("com/portname", ui->portNameComboBox->currentText());
    Ini.setValue("com/baudrate", ui->baudRateComboBox->currentText());
    Ini.setValue("com/timeout", ui->checkBoxTimeout->checkState());
    Ini.setValue("sys/day", ui->spinBox_Day->value());
    Ini.setValue("sys/mp3", ui->comboBox->currentIndex());
    Ini.setValue("sys/mp3_random", ui->rb_random->isChecked());
    Ini.setValue("com/delytime", ui->spinBoxPoll->value());
    Ini.setValue("net/hostname", ui->editHostName->text());
    Ini.setValue("net/port", ui->spinPort->value());
    Ini.setValue("net/username", ui->editUserName->text());
    Ini.setValue("net/topic", ui->editTopic->text());
    Ini.setValue("net/password", ui->editPassword->text());
    Ini.setValue("net/tb", ui->checkThingsBoard->checkState());

    for(int i = 1; i <= MAX_CARD_COUNT; i++)
    {
        Ini.setValue("msa/addr" + QString::number(i), standItemModel->item(i - 1, 1)->text());
        Ini.setValue("msa/a" + QString::number(i), standItemModel->item(i - 1, 2)->text());
        Ini.setValue("msa/b" + QString::number(i), standItemModel->item(i - 1, 3)->text());
    }

    emit paramChanged(); // 参数已改变
    this->close();
}

void options::on_btn_cancel_clicked()
{
    this->close();
}

void options::on_pushButton_clicked()
{
#ifdef Q_OS_LINUX
    QProcess::startDetached("python3 /usr/bin/onboard");
#endif
}

void options::add_mp3(QList<QFileInfo>* fileInfo)
{
    QSettings Ini(QCoreApplication::applicationDirPath() + "/config.ini", QSettings::IniFormat, this);
    ui->comboBox->clear();

    for(int i = 0; i < fileInfo->count(); i++)
    {
        ui->comboBox->addItem(fileInfo->at(i).fileName());
    }

    ui->comboBox->setCurrentIndex(Ini.value("sys/mp3", 0).toInt());
    bool b = Ini.value("sys/mp3_random", false).toBool();
    ui->rb_random->setChecked(b);
    ui->rb_assign->setChecked(!b);
}



void options::on_pushButton_playSound_clicked()
{
    emit testWarningSound(ui->comboBox->currentIndex());
}
